package com.farecrawler.app.utils;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

import com.farecrawler.app.constants.Constants;

public class EmailUtils {
	
	public static final String GMAIL_EMAIL = "zing.app.airfare@gmail.com";
	public static final String DEFAULT_SUBJECT_NAME = "Airfare Report";
	
	private static final String PASSWORD = "nopa55w0rd";
	private static final String HOST_NAME = "smtp.gmail.com";
	private static final Integer HOST_PORT = 587;
	
	/**
	 * Sending a Html-formatted message to the app's Gmail account
	 * 
	 * @param subject
	 * @param htmlMsg
	 * @param isDebug
	 * @throws EmailException
	 * 
	 * @author Alvin Zing
	 */
	public static void sendHtmlEmail(String subject, String htmlMsg, boolean isDebug) throws EmailException {
		
		HtmlEmail email = new HtmlEmail();
		email.setSmtpPort(HOST_PORT);
		email.setAuthenticator(new DefaultAuthenticator(GMAIL_EMAIL, PASSWORD));
		email.setDebug(isDebug);
		email.setHostName(HOST_NAME);
		email.setFrom(GMAIL_EMAIL, Constants.APP_NAME);
		email.setSubject(subject);
		email.setHtmlMsg(htmlMsg);
		email.addTo(GMAIL_EMAIL);
		email.setTLS(true);
		
		email.send();
	}
	
	public static void sendHtmlEmail(String subject, String htmlMsg) throws EmailException {
		sendHtmlEmail(subject, htmlMsg, false);
	}


}
